/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Arrays;
import java.util.Random;

/**
 * <p>Project: app2022 - ArrSort1
 * <p>Powered by webrx On 2022-01-08 16:26:29
 *
* @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ArrSort2 {
    public static void main(String[] args) {
        //冒泡排序
        var rand = new Random();
        var n = new int[10];
        for (int i = 0; i < n.length; i++) {
            n[i] = rand.nextInt(100) + 1;
        }
        System.out.printf("排序前：%s%n", Arrays.toString(n));

        //冒泡升序排序
        for (int i = 0; i < n.length - 1; i++) {
            for (int j = 0; j < n.length - i - 1; j++) {
                // >大于 就升序  <小点于就是降序
                if (n[j] > n[j + 1]) {
                    int t = n[j];
                    n[j] = n[j + 1];
                    n[j + 1] = t;
                }
            }
        }
        System.out.printf("排序升后：%s%n", Arrays.toString(n));


        //冒泡降序排序
        for (int i = 0; i < n.length - 1; i++) {
            for (int j = 0; j < n.length - i - 1; j++) {
                // >大于 就升序  <小点于就是降序
                if (n[j] < n[j + 1]) {
                    int t = n[j];
                    n[j] = n[j + 1];
                    n[j + 1] = t;
                }
            }
        }
        System.out.printf("排序降后：%s%n", Arrays.toString(n));

        //乱序排序
        for (int x = 0; x < 5; x++) {
            for (int i = 0; i < n.length - 1; i++) {
                for (int j = 0; j < n.length - i - 1; j++) {
                    // >大于 就升序  <小点于就是降序
                    if (rand.nextBoolean()) {
                        int t = n[j];
                        n[j] = n[j + 1];
                        n[j + 1] = t;
                    }
                }
            }
        }
        System.out.printf("排序乱后：%s%n", Arrays.toString(n));
    }
}
